<?php
include("localConfig.php");
include("DB.php");
session_start();
// php对象转json
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {

            $array[$key] = $function($value);
        }
        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}

// html字符处理
function html_str_replace($str)
{
  for($i = 0;$i<100;$i++){
    $str = str_replace(">","&gt;",$str);
    $str = str_replace("<","&lt;",$str);
    $str = str_replace("'","&#39;",$str);
    $str = str_replace('"','&quot;',$str); 
  }
  return $str; 
}

//去掉所有空格
function trims($str){
    $str = str_replace(" ","",$str);
    return $str;
}

//查询单条数据
function select_Object($sql){
    $result = mysql_query($sql);
    if(!is_resource($result)) {
        // trigger_error($result.'不是一个有效的资源');
        return false;
    }else{
        $object = mysql_fetch_array($result);
        mysql_free_result($result);
        return $object;
    }
    return false;

}

//查询多条数据
function select_ObjectList($sql){
    $result = mysql_query($sql);
    if(!is_resource($result)) {
        // trigger_error($result.'不是一个有效的资源');
        return false;
    }
    while($row = mysql_fetch_assoc($result)) {
        $arr[]=$row;
    }
    mysql_free_result($result);
    // mysql_close();
    if(!is_array($arr)){
        $arr = array();
    }
    return $arr;
}


//插入单条数据
function insert_Object($sql){
    mysql_query($sql);
    $rows = mysql_affected_rows();
    mysql_close();
    return $rows;
}

//更新单条数据
function update_Object($sql){
    mysql_query($sql);
    $rows = mysql_affected_rows();
    mysql_close();
    return $rows;
}

//删除单条数据
function delete_Object($sql){
    mysql_query($sql);
    $rows = mysql_affected_rows();
    mysql_close();
    return $rows;
}

/**
 * php加密解密中间算法
 * @param $txt              加密/解密文本
 * @param $encrypt_key      加密因子
 * @return $tmp             返回值
 */
function keyED($txt, $encrypt_key) {
    $encrypt_key = md5 ( $encrypt_key );
    $ctr = 0;
    $tmp = "";
    for($i = 0; $i < strlen ( $txt ); $i ++) {
        if ($ctr == strlen ( $encrypt_key ))
            $ctr = 0;
        $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 );
        $ctr ++;
    }
    return $tmp;
}

/**
 * php加密算法
 * @param $txt              加密/解密文本
 * @param $encrypt_key      加密因子
 */    
function Encrypt($txt, $key) {
    $encrypt_key = md5("19871023");
    $ctr = 0;
    $tmp = "";
    for($i = 0; $i < strlen ( $txt ); $i ++) {
        if ($ctr == strlen ( $encrypt_key ))
            $ctr = 0;
        $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ));
        $ctr ++;
    }
    $_code = md5 ( $encrypt_key ) . base64_encode ( keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key );
    return true? $_code : null;
} 

/**
 * php解密算法
 * @param $txt              加密/解密文本
 * @param $encrypt_key      加密因子
 */    
function Decrypt($txt, $key) {
    $txt = keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key );
    $tmp = "";
    for($i = 0; $i < strlen ( $txt ); $i ++) {
        $md5 = substr ( $txt, $i, 1 );
        $i ++;
        $tmp .= (substr ( $txt, $i, 1 ) ^ $md5);
    }
    return true? $tmp : null;
}  

?>
